package com.hjj.services;

import com.hjj.beans.Activity;
import com.hjj.dto.ResultPageDTO;
import com.hjj.mapper.ActivityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Locale;
import java.util.UUID;

@Service
public class ActivityServicesImp implements ActivityServices{
    @Autowired
    ActivityMapper activityMapper;

    @Override
    public ArrayList<Activity> getAll(ResultPageDTO resultPageDTO) {
        // 获取当前页
        int currentPage = resultPageDTO.getCurrentPage();
        // 获取每页记录条数
        int rowsPerPage = resultPageDTO.getRowsPerPage();

        // 计算当前页的起始值
        int result = (currentPage - 1) * rowsPerPage;

        // 将当前页的起始值添加到当前页的变量中
        resultPageDTO.setCurrentPage(result);


        // 获取所有市场活动数据的条数
        int count = activityMapper.getCount(resultPageDTO);

        // 将市场数据总条数添加到总记录条数中
        resultPageDTO.setTotalRows(count);


        // 获取能显示的页数
        int pagination = count / rowsPerPage;

        // 如果取余还有余数页数需要加一
        if (count % rowsPerPage != 0){
            pagination += 1;
        }

        // 设置总页数
        resultPageDTO.setTotalPages(pagination);

        return activityMapper.getAll(resultPageDTO);
    }

    @Override
    public int getCount(ResultPageDTO resultPageDTO) {
        return activityMapper.getCount(resultPageDTO);
    }

    @Override
    public Activity getIdAndRemark(String id) {
        return activityMapper.getById(id);
    }

    @Override
    public boolean add(Activity activity) {
        // 给市场活动数据添加UUID
        activity.setId(UUID.randomUUID().toString().replace("-", ""));

        // 过市场数据添添加创建时间
        activity.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        return activityMapper.add(activity) > 0 ? true : false;
    }


    @Override
    public Activity editBefore(String id) {
        return activityMapper.editBefore(id);
    }

    @Override
    public boolean edit(Activity activity) {
        // 过市场数据添添加创建时间
        activity.setEditTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        return activityMapper.edit(activity) > 0 ? true : false;
    }

    @Override
    public boolean del(String[] ids) {
        return activityMapper.del(ids) > 0 ? true : false;
    }
}
